﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;

namespace TowerDef
{
    public class ObjectContainer
    {
        ContentManager content;
        List<GameObject> list;

        public ObjectContainer(ContentManager content)
        {
            this.content = content;
            this.list = new List<GameObject>();
        }

        public void loadContent()
        {

        }

        public void draw()
        {
            foreach (GameObject o in list)
            {
                o.draw();
            }
        }

        public void update()
        {
            GameObject toDelete = new ObjectNonSpecified();
            foreach (GameObject o in list)
            {
                o.update();
                if (o.type == ObjectType.ENEMY)
                {
                    ObjectEnemy oe = (ObjectEnemy)o;
                    if (oe.reachedCheckPoint())
                    {
                        GameObject test = lastCheckPoint();
                        if (test.positionOnMap.x == oe.positionOnMap.x && test.positionOnMap.y == oe.positionOnMap.y)
                        {
                            toDelete = oe;
                        }
                        else
                        {
                            oe.checkPointsCounter++;
                            oe.nextCheckPoint = (PathCheckPoint)findCheckPoint(oe.checkPointsCounter);
                            oe.direction = oe.nextCheckPoint.directionToReach;
                        }
                    }
                }
            }
            if(toDelete.type != ObjectType.NOT_SPECIFIED)
                list.Remove(toDelete);
        }

        public GameObject addObject(ObjectType objectType, Vector3 pos, Position positionOnMap)
        {
            switch (objectType)
            {
                case ObjectType.ENEMY:
                    GameObject o = new ObjectEnemy(EnemyType.ST1);
                    o.loadContent(content);
                    o.modelPosition = new Vector3(pos.X, pos.Y, pos.Z);
                    o.positionOnMap = new Position(positionOnMap.x, positionOnMap.y);
                    list.Add(o);
                    return o;
                case ObjectType.CHECKPOINT:
                    GameObject c = new PathCheckPoint();
                    c.loadContent(content);
                    c.modelPosition = pos;
                    c.positionOnMap = positionOnMap;
                    list.Add(c);
                    return c;
            }
            return null;
        }

        public Position firstCheckPointPosition()
        {
            Position position;
            GameObject firstCheckPoint = new PathCheckPoint();

            foreach (GameObject o in list)
            {
                if (o.type == ObjectType.CHECKPOINT)
                {
                    firstCheckPoint = o;
                    break;
                }                    
            }
            position = new Position(firstCheckPoint.positionOnMap.x, firstCheckPoint.positionOnMap.y);

            return position;
        }

        public GameObject firstCheckPoint()
        {
            foreach (GameObject o in list)
            {
                if (o.type == ObjectType.CHECKPOINT)
                {
                    return o;
                }
            }
            return null;
        }

        public GameObject findCheckPoint(int i)
        {
            for (int counter = 0; counter < list.Count; counter++)
            {
                if (list[counter].type == ObjectType.CHECKPOINT)
                {
                    if (counter == i)
                        return list[counter];
                }
            }
            return null;
        }

        public GameObject lastCheckPoint()
        {
            GameObject go = new ObjectNonSpecified();
            foreach (GameObject o in list)
            {
                if (o.type == ObjectType.CHECKPOINT)
                    go = o;
            }
            return go;
        }
    }
}
